package com.aldjob.controllerImpl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.omg.PortableServer.REQUEST_PROCESSING_POLICY_ID;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.project.XXXX.api.SessionContext;
import com.project.XXXX.inter.*;
import com.project.XXXX.model.*;
import com.project.XXXX.tools.ProjectTools;

import sun.misc.BASE64Decoder;

@Controller

public class ProfileController {

	private tbl_sysuserMapper tbl_sysuserMapper;
	private tbl_employeeinfoMapper tbl_employeeinfoMapper;
	private tbl_employerinfoMapper tbl_employerinfoMapper;

	public ProfileController(com.project.XXXX.inter.tbl_sysuserMapper tbl_sysuserMapper,
			com.project.XXXX.inter.tbl_employeeinfoMapper tbl_employeeinfoMapper,
			com.project.XXXX.inter.tbl_employerinfoMapper tbl_employerinfoMapper) {
		super();
		this.tbl_sysuserMapper = tbl_sysuserMapper;
		this.tbl_employeeinfoMapper = tbl_employeeinfoMapper;
		this.tbl_employerinfoMapper = tbl_employerinfoMapper;
	}

	public tbl_employeeinfo ProfileSelectHandler(HttpSession session) {
		tbl_employeeinfo tbl_employeeinfo = null;
		// System.out.println(session+" inin ");
		if (session != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
			tbl_employeeinfo = tbl_employeeinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
			
			
		}
		return tbl_employeeinfo;
	}

	public int ProfileUpdateHandler(HttpServletRequest request, HttpSession sess) {

		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			tbl_employeeinfo tbl_employeeinfo = tbl_employeeinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
			tbl_employeeinfo.setMobile(tbl_sysuser.getMobile());
			tbl_employeeinfo.setCreatetime(tbl_sysuser.getCreatetime());
			Date birthday = null;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小写的mm表示的是分钟
			try {
				birthday = sdf.parse(request.getParameter("birthday"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			tbl_employeeinfo.setBirthday(birthday);
			tbl_employeeinfo.setEmail(request.getParameter("email"));
			tbl_employeeinfo.setGender(request.getParameter("gender"));
			tbl_employeeinfo.setInschoolid(
					request.getParameter("inschoolid") == null ? "1" : request.getParameter("inschoolid"));
			tbl_employeeinfo.setTellus(request.getParameter("tellus"));
			tbl_employeeinfo.setSchool(request.getParameter("school"));
			tbl_employeeinfo.setMajor(request.getParameter("major"));
			tbl_employeeinfo.setModifytime(new Date());
			tbl_employeeinfo.setPwd(tbl_sysuser.getPwd());
			tbl_employeeinfo.setRealname(request.getParameter("realname"));
			tbl_employeeinfo.setTheyear(request.getParameter("theyear"));
			tbl_employeeinfo.setUsername(request.getParameter("username"));
			tbl_employeeinfo.setWechatid(request.getParameter("wechatid"));
			String _bookpttypeids[] = request.getParameterValues("bookpttypeids");
			if (_bookpttypeids != null && _bookpttypeids.length > 0) {
				String _booked = "";
				// System.out.println("_bookpttypeids l:" +
				// _bookpttypeids.length);
				for (int i = 0; i < _bookpttypeids.length; i++) {
					_booked = _booked + (_bookpttypeids[i] + '&');
					// System.out.println(_booked);
				}
				// System.out.println(_booked);
				tbl_employeeinfo.setBookpttypeids(_booked);
			}
			tbl_employeeinfoMapper.updateByPrimaryKey(tbl_employeeinfo);
			tbl_sysuser.setEmail(request.getParameter("email"));
			tbl_sysuser.setModifytime(new Date());
			if (request.getParameter("username") != null)
				tbl_sysuser.setNickname(request.getParameter("username"));
			else
				tbl_sysuser.setNickname(request.getParameter("realname"));
			tbl_sysuser.setUsername(request.getParameter("realname"));
			tbl_sysuserMapper.updateByPrimaryKey(tbl_sysuser);
			sess.setAttribute("User", tbl_sysuser);
			return 200;
		} else {
			return 403;
		}
	}

	public int FinishERInfo(HttpServletRequest request, HttpSession sess) {
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			tbl_employerinfo tbl_employerinfo = tbl_employerinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
			tbl_employerinfo.setMobile(tbl_sysuser.getMobile());
			tbl_employerinfo.setCreatetime(tbl_sysuser.getCreatetime());
			tbl_employerinfo.setComment(request.getParameter("comment"));
			// tbl_employeeinfo.setCreator(request.getParameter(""));
			// tbl_employeeinfo.setCreatorid(tbl_sysuser.getMobile());
			// tbl_employeeinfo.setDeletetime(request.getParameter(""));
			tbl_employerinfo.setEmail(request.getParameter("email"));
			tbl_employerinfo.setDivisionid(request.getParameter("s_county"));
			tbl_employerinfo.setMaibox(tbl_sysuser.getEmail());
			tbl_employerinfo.setScale(request.getParameter("scale"));
			tbl_employerinfo.setTel(request.getParameter("tel"));
			tbl_employerinfo.setModifytime(new Date());
			tbl_employerinfo.setPwd(tbl_sysuser.getPwd());
			tbl_employerinfo.setName(request.getParameter("name"));
			tbl_employerinfo.setAddress(request.getParameter("address"));
			// System.out.println(request.getParameter("s_county"));
			tbl_employerinfo.setWechatid(request.getParameter("wechatid"));
			tbl_employerinfo.setContactor(request.getParameter("contactor"));
			tbl_employerinfoMapper.updateByPrimaryKey(tbl_employerinfo);
			tbl_sysuser.setModifytime(new Date());
			tbl_sysuserMapper.updateByPrimaryKey(tbl_sysuser);
			return 200;
		} else
			return 403;

	}

	public tbl_employerinfo ShowERInfo(HttpSession session) {
		tbl_employerinfo tbl_employerinfo = null;
		if (session != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
			tbl_employerinfo = tbl_employerinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
		}
		return tbl_employerinfo;
	}

	public int employerLogoString(HttpServletRequest request, HttpSession session, String pic1) {
		if (session != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
			tbl_employerinfo tbl_employerinfo = tbl_employerinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
			File f;
			try {
				String rootPath = getClass().getResource("./").getFile().toString();

				rootPath = rootPath.substring(0, rootPath.indexOf("WEB-INF"));
				File dir = new File(rootPath + "/employer/logo/");
				String name ="defaultlogo";
				String path = FilenameUtils.separatorsToSystem(dir + "/" + name + ".jpg");
				if(StringUtils.equals(tbl_employerinfo.getLogo(), "/employer/logo/defaultlogo.jpg")||StringUtils.isBlank(tbl_employerinfo.getLogo()))
				{
					 name = RandomStringUtils.random(10, true, true)+System.currentTimeMillis()+ ".jpg";
					 path = FilenameUtils.separatorsToSystem(dir + "/" + name  );
					 tbl_employerinfo.setLogo("/employer/logo/" + name);
					 tbl_employerinfoMapper.updateByPrimaryKey(tbl_employerinfo);
				}
				else {				
					path=FilenameUtils.separatorsToSystem(path=rootPath + tbl_employerinfo.getLogo());
				}
				
				f = new File(path);
				tbl_employerinfo.setLogo("/employer/logo/" + name + ".jpg");
				if (!dir.exists())
					dir.mkdirs();

				if (!f.exists())
					f.createNewFile();
				FileOutputStream fout1 = null;
				fout1 = new FileOutputStream(f);
				fout1.write(new BASE64Decoder().decodeBuffer(pic1));
				fout1.close();
				tbl_employerinfoMapper.updateByPrimaryKey(tbl_employerinfo);
				session.setAttribute("ERinfo", tbl_employerinfo);
				return 200;
			} catch (IOException e) {
				return 409;
			}
		} else
			return 403;

	}

	public int employeeLogoString(HttpServletRequest request, HttpSession session, String pic1) {
		if (session != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
			tbl_employeeinfo tbl_employeeinfo = tbl_employeeinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
			File f;
			try {
				String rootPath = getClass().getResource("./").getFile().toString();

				rootPath = rootPath.substring(0, rootPath.indexOf("WEB-INF"));
				File dir = new File(rootPath + "/employee/logo/");
				String name ="default";
				String path = FilenameUtils.separatorsToSystem(dir + "/" + name + ".jpg");
				if(StringUtils.equals(tbl_employeeinfo.getLabelid(), "/employee/logo/default.jpg")||StringUtils.isBlank(tbl_employeeinfo.getLabelid()))
				{
					 name = RandomStringUtils.random(10, true, true)+System.currentTimeMillis()+ ".jpg";
					 path = FilenameUtils.separatorsToSystem(dir + "/" + name  );
					 tbl_employeeinfo.setLabelid ("/employee/logo/" + name);
					 tbl_employeeinfoMapper.updateByPrimaryKey(tbl_employeeinfo);
				}
				else {				
					path=FilenameUtils.separatorsToSystem(path=rootPath + tbl_employeeinfo.getLabelid());
				}
				f = new File(path);
				tbl_employeeinfo.setLabelid("/employee/logo/" + name + ".jpg");
				if (!dir.exists())
					dir.mkdirs();

				if (!f.exists())
					f.createNewFile();
				FileOutputStream fout1 = null;
				fout1 = new FileOutputStream(f);
				fout1.write(new BASE64Decoder().decodeBuffer(pic1));
				fout1.close();
				tbl_employeeinfoMapper.updateByPrimaryKey(tbl_employeeinfo);
				session.setAttribute("ERinfo", tbl_employeeinfo);
				return 200;
			} catch (IOException e) {
				return 409;
			}
		} else
			return 403;

	}
}